package com.example.aitoedu.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.aitoedu.constant.MessageConstant;
import com.example.aitoedu.dto.PlDTO;
import com.example.aitoedu.dto.PwdChangeDTO;
import com.example.aitoedu.entity.Student;
import com.example.aitoedu.entity.Teacher;
import com.example.aitoedu.entity.User;
import com.example.aitoedu.exception.UploadErrorException;
import com.example.aitoedu.mapper.TeacherMapper;
import com.example.aitoedu.mapper.UserMapper;
import com.example.aitoedu.service.TeacherService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.aitoedu.utils.AliOSSUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDateTime;

/**
 * @author 曹正豪
 * @description 针对表【teacher(教师信息)】的数据库操作Service实现
 * @createDate 2024-08-26 23:43:01
 */
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher>
    implements TeacherService{

    @Autowired
    private TeacherMapper teacherMapper;

    @Autowired
    private AliOSSUtils aliOSSUtils;

    @Autowired
    private UserMapper userMapper;

    @Override
    public Teacher pl() {
        Long tid = Long.valueOf((String) StpUtil.getLoginId());
        Teacher teacher = teacherMapper.selectById(tid);
        if(ObjectUtils.isEmpty(teacher)) return new Teacher();
        teacher.setPassword("******");
        return teacher;
    }

    @Override
    public void update_picture(MultipartFile file) {
        Long tid = Long.valueOf((String) StpUtil.getLoginId());
        String upload = null;
        try {
            upload = aliOSSUtils.upload(file);
        } catch (IOException e) {
            throw new UploadErrorException(MessageConstant.UPLOAD_ERROR);
        }
        QueryWrapper queryWrapper = new QueryWrapper<>().eq("id", tid);
        LocalDateTime now = LocalDateTime.now();
        Teacher teacher = Teacher.builder()
                .picture(upload)
                .updateTime(now)
                .build();
        User user = new User();
        user.setUpdateTime(now);
        teacherMapper.update(teacher,queryWrapper);
        userMapper.update(user, queryWrapper);
    }

    @Override
    public SaResult update_pwd(PwdChangeDTO pwdChangeDTO) {
        Long tid = Long.valueOf((String) StpUtil.getLoginId());
        Teacher teacher = teacherMapper.selectById(tid);
        String s = DigestUtils.md5DigestAsHex(pwdChangeDTO.getNewPwd().getBytes());
        if(!teacher.getPassword().equals(DigestUtils.md5DigestAsHex(pwdChangeDTO.getOldPwd().getBytes()))){
            return SaResult.error("旧密码错误");
        }
        if (teacher.getPassword().equals(s)){
            return SaResult.error(MessageConstant.PWD_EQUAL);
        }
        if (!pwdChangeDTO.getNewPwd().equals(pwdChangeDTO.getSecPwd())){
            return SaResult.error(MessageConstant.PWD_NOTEQUAL);
        }
        teacher.setPassword(DigestUtils.md5DigestAsHex(pwdChangeDTO.getNewPwd().getBytes()));
        LocalDateTime now = LocalDateTime.now();
        teacher.setUpdateTime(now);
        teacherMapper.updateById(teacher);
        User user = userMapper.selectById(tid);
        user.setPassword(DigestUtils.md5DigestAsHex(pwdChangeDTO.getNewPwd().getBytes()));
        user.setUpdateTime(now);
        userMapper.updateById(user);
        return SaResult.ok("修改成功");
    }

    @Override
    public SaResult update_pl(PlDTO plDTO) {
        Long id = Long.valueOf(StpUtil.getLoginId().toString());
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        Teacher teacher = Teacher.builder()
                .school(plDTO.getSchool())
                .sex(plDTO.getSex())
                .tname(plDTO.getSname())
                .build();
        LocalDateTime now = LocalDateTime.now();
        teacher.setUpdateTime(now);
        teacherMapper.update(teacher, queryWrapper);
        User user = new User();
        user.setUpdateTime(now);
        userMapper.update(user, queryWrapper);
        return SaResult.ok();
    }
}




